from rest_framework.decorators import action
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response


from .serializers import OrderSerializer
from orders.models import OrderInfo
from .paginations import PageNum

class OrderView(ModelViewSet):
    queryset = OrderInfo.objects.all()
    serializer_class = OrderSerializer
    pagination_class = PageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return self.queryset.filter(order_id__contains=keyword)
        else:
            return self.queryset.all()


    @action(methods=['put'],detail=True)
    def status(self,request,pk):
        order = self.get_object()
        # status = request.data.get('status')
        # order.status = status
        # order.save()
        # ser = self.get_serializer(order)
        # return Response({
        #     'order_id':order.order_id,
        #     'status': order.status
        # })
        ser = self.get_serializer(instance=order,data=request.data,partial=True)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response(ser.data)

